package com.yc.spring.jdbc.dao;

import com.yc.spring.jdbc.bean.Account;
import com.yc.spring.jdbc.bean.Record;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 银行账号流水表
 */
@Repository
@Lazy
public class RecordDao {

    public RecordDao() {
        System.out.println("========= RecordDao 加载完成!!! ===========");
    }

    @Resource
    private JdbcTemplate jt;

    /**
     * 添加流水记录
     *
     * @param record
     */
    public void insert(Record record) {
        String sql = "---------------------insert into oprecord values(default,?,?,now())";
        jt.update(sql, record.getAccountid(), record.getOpmoney());
    }

    /**
     * 根据账号查询流水记录
     *
     * @param accountid
     * @return
     */
    public List<Record> selectByAccountid(Integer accountid) {
        String sql = "select * from oprecord where accountid = ?";
        return jt.query(sql, (rs, i) -> {
            Record a = new Record();
            a.setId(rs.getInt("id"));
            a.setAccountid(rs.getInt("accountid"));
            a.setOpmoney(rs.getDouble("opmoney"));
            a.setOptime(rs.getTimestamp("optime"));
            return a;
        }, accountid);
    }

}
